__author__ = '荣耀燃尽  世界尽头'
userlist=[
    {'name':'zhangsan','password':'111','balance':3300},
    {'name':'lisi','password':'222','balance':4000},
    {'name':'zhaowu','password':'333','balance':5040}
    ]
namelist=["zhangsan",'lisi','zhaowu']
def GetNameIndex(name):
    '''
    找到对应账户名为第几个账户
    :param name:
    :return:
    '''
    for i in range(len(userlist)):
        if userlist[i]['name']==name:
            nameid=i
            return nameid
def ShowBalance(name):
    '''
    通过账户姓名查询账户余额
    :param name: 账户姓名
    :return:账户余额
    '''
    nameid=GetNameIndex(name)
    print("*"*16+"当前账户信息"+"*"*16)
    print("| name:{0:<8}| balance:{1:<4} |".format(userlist[nameid]['name'],userlist[nameid]['balance']))
    print("*"*44)
#设置一个状态，表示当前是已登陆还是未登陆
state=0
while True:
    #输出初始界面
    while state==0:
        print("*"*15+"自动提款机系统"+"*"*15)
        name=input("请输入账户名:")
        if name in namelist:
            password=input("请输入密码:")
            nameid=GetNameIndex(name)
            if userlist[nameid]['password']!=password:
                print("输入的密码和账户不符，请重新输入！")
            else:
                state=1
                print("*"*44)
        else:
            print("当前账户名不存在！！")


    print("{0:1} {1:<13} {2:15}".format(" ","1.查询余额","2.取款"))
    print("{0:1} {1:<15} {2:15}".format(" ","3.存款","4.退出"))
    print("*"*44)
    key=input("请输入对应的选择：")
    #根据键盘值，判断并执行对应的操作
    if key=='1':
        ShowBalance(name)
    elif key=='2':
        print("最高可取金额:",userlist[nameid]['balance'])
        takeMoney=int(input("请输入要取出的金额："))
        while takeMoney>userlist[nameid]['balance']:
            print("你要取出的金额已超出余额，请输入有效金额！")
            takeMoney=int(input("请输入要取出的金额："))
        userlist[nameid]['balance']-=takeMoney
        ShowBalance(name)
    elif key=='3':
        deposit=int(input("请输入要存入的金额"))
        userlist[nameid]['balance']+=deposit
        ShowBalance(name)
    elif key=='4':
        state=0
        print("正在退出当前账户。。。")
        print("再见！！")
    else:
        print("*"*20+"无效的输入"+"*"*20)
